/**
 * This code is released under lgpl v3 license.
 * All rights reserved.
 */
package net.gfbridge.demo.client;

import bridge.FABridge;
import bridge.FABridgeInstance;
import bridge.FABridgeWrapper;
import bridge.InitCallback;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.FocusEvent;
import com.google.gwt.event.dom.client.FocusHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import flash.display.Sprite;
import flash.media.Sound;
import flash.media.SoundChannel;
import flash.net.URLRequest;
import pl.rmalinowski.gwt2swf.client.ui.SWFWidget;

/**
 *
 * @author yuan yunchang    dukerr@gmail.com
 */
public class SoundDemo implements EntryPoint {

    String bridgeName = "soundDemo";
    TextBox urlInput;
    Button pause;
    Button playSound;
    boolean isPlaying = false;
    double position;
    SoundChannel channel;
    Sound sound;
    URLRequest soundReq;
    Sprite root;
    FABridgeInstance bridgeInstance;
    VerticalPanel mainLayout;

    public void onModuleLoad() {
        mainLayout = new VerticalPanel();
        mainLayout.setWidth("100%");
        RootPanel.get("sound-demo").add(mainLayout);
        String baseUrl = GWT.getModuleBaseURL();
        SWFWidget swf = new SWFWidget(baseUrl + "EmptyFABridgeSWF.swf", "1px", "1px");
        swf.addFlashVar("bridgeName", bridgeName);

        mainLayout.add(swf);
        FABridgeWrapper.get().addInitCallback(bridgeName, new InitCallback() {

            public void initComplete(FABridgeInstance inst) {
                //Window.alert("sound init complete");
                bridgeInstance = inst;
                root = (Sprite) bridgeInstance.root();
                createSoundDemo();
            }
        });
    }

    void clearChannel() {
        if (channel != null) {
            bridgeInstance.releaseNamedASObject(channel);
            bridgeInstance.releaseNamedASObject(channel);
        }
    }

    void clearSoundReq() {
        if (soundReq != null) {
            bridgeInstance.releaseNamedASObject(soundReq);
        }
    }

    void clearSound() {
        if (sound != null) {
            bridgeInstance.releaseNamedASObject(sound);
        }
    }

    void createSoundDemo() {
        //add input sound
        HorizontalPanel soundpanel = new HorizontalPanel();

        RootPanel.get("sound-demo").add(soundpanel);
        Label urlLabel = new Label("sound url:");
        soundpanel.add(urlLabel);
        urlInput = new TextBox();
        urlInput.setWidth("500px");
        urlInput.addFocusHandler(new FocusHandler() {

            public void onFocus(FocusEvent event) {
                urlInput.selectAll();
            }
        });
        soundpanel.add(urlInput);

        //play sound demo
        playSound = new Button("play sound");
        soundpanel.add(playSound);
        playSound.addClickHandler(new ClickHandler() {

            public void onClick(ClickEvent event) {
                if (channel != null) {
                    channel.stop();
                    clearChannel();
                    isPlaying = false;
                    channel = null;
                }

                clearSoundReq();
                String url = urlInput.getText();
                soundReq = URLRequest.createURLRequest(bridgeName);
                soundReq.setUrl(url);

                clearSound();
                sound = Sound.createSound(bridgeName);
                sound.load(soundReq);
                channel = sound.play(0, 1);
                isPlaying = true;
            }
        });
        //stop button
        Button stopPlay = new Button("stop");
        stopPlay.addClickHandler(new ClickHandler() {

            public void onClick(ClickEvent event) {
                if (channel != null) {
                    position = 0;
                    channel.stop();
                    clearChannel();
                    channel = null;
                    isPlaying = false;
                }
            }
        });
        soundpanel.add(stopPlay);

        pause = new Button("pause/continue");
        pause.addClickHandler(new ClickHandler() {

            public void onClick(ClickEvent event) {
                if (isPlaying) {
                    position = channel.getPosition();
                    channel.stop();
                    clearChannel();
                    isPlaying = false;
                    channel = null;
                } else {
                    channel = sound.play(position, 1);
                    isPlaying = true;
                }

            }
        });
        soundpanel.add(pause);
    }
}
